﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;


namespace MyLibrary.TaskPlan.Service
{
    /// <summary>
    /// 数据库中的计划任务实体
    /// </summary>
    public class TaskInDatabase
    {
        private string _taskName = String.Empty;
        /// <summary>
        /// 计划任务名称
        /// </summary>
        public string TaskName
        {
            get { return _taskName; }
            set { _taskName = value; }
        }
        private DateTime _nextRuntime = DateTime.Now;
        /// <summary>
        /// 下次运行时间
        /// </summary>
        public DateTime NextRuntime
        {
            get { return _nextRuntime; }
            set { _nextRuntime = value; }
        }
        private DateTime _lastRuntime = DateTime.Now;
        /// <summary>
        /// 最后运行时间
        /// </summary>
        public DateTime LastRuntime
        {
            get { return _lastRuntime; }
            set { _lastRuntime = value; }
        }
        private string _status = String.Empty;
        /// <summary>
        /// 当前状态
        /// </summary>
        public string Status
        {
            get { return _status; }
            set { _status = value; }
        }
        private string _lastResult = String.Empty;
        /// <summary>
        /// 最后运行结果
        /// </summary>
        public string LastResult
        {
            get { return _lastResult; }
            set { _lastResult = value; }
        }
        private string _creator = String.Empty;
        /// <summary>
        /// 计划任务建立者
        /// </summary>
        public string Creator
        {
            get { return _creator; }
            set { _creator = value; }
        }
    }

    /// <summary>
    /// 计划任务触发器转换
    /// </summary>
    [Serializable]
    public class TaskTriggerParse
    {
        private TaskTriggerList _triggerList = null;
        [XmlElementAttribute("Trigger")]
        public TaskTriggerList TriggerList
        {
            get { return _triggerList; }
            set { _triggerList = value; }
        }

        /// <summary>
        /// 将XML格式的触发器配置节转为触发器实体
        /// </summary>
        /// <param name="triggerContent">Xml格式的触发器配置节</param>
        /// <returns>触发器实体</returns>
        public static TaskTriggerParse LoadTriggers(string triggerContent)
        {
            XmlSerializer xs = new XmlSerializer(typeof(TaskTriggerParse));
            TextReader reader = new StringReader(triggerContent);
            TaskTriggerParse rc = xs.Deserialize(reader) as TaskTriggerParse;
            return rc;
        }

        /// <summary>
        /// 返回触发器的XML格式
        /// </summary>
        /// <returns></returns>
        public string GetTriggers()
        {
            StringBuilder builder = new StringBuilder();
            XmlSerializer xs = new XmlSerializer(typeof(TaskTriggerParse));
            TextWriter writer = new StringWriter(builder);
            xs.Serialize(writer, this);
            return writer.ToString();
        }
    }

    /// <summary>
    /// 计划任务触发器列表
    /// </summary>
    public class TaskTriggerList : List<TaskTrigger>
    {
    }

    /// <summary>
    /// 计划任务触发器
    /// </summary>
    [Serializable]
    public class TaskTrigger
    {
        private string _triggerType = String.Empty;
        /// <summary>
        /// 触发器类型
        /// </summary>
        [XmlAttributeAttribute("type")]
        public string TriggerType
        {
            get { return _triggerType; }
            set { _triggerType = value; }
        }

        private string _startTime = String.Empty;
        /// <summary>
        /// 触发器开始时间
        /// </summary>
        [XmlElementAttribute("StartTime")]
        public string TriggerRunTime
        {
            get { return _startTime; }
            set { _startTime = value; }
        }

        private string _period = String.Empty;
        /// <summary>
        /// 触发器运行周期
        /// </summary>
        [XmlElementAttribute("Period")]
        public string Period
        {
            get { return _period; }
            set { _period = value; }
        }

        private string _number = String.Empty;
        [XmlAttributeAttribute("number")]
        public string Number
        {
            get { return _number; }
            set { _number = value; }
        }
    }
}
